function textReplace(searchText, replaceText) {
  if (documents.length > 0) {
    var activeDocument = app.activeDocument;
    var elementsSelected = activeDocument.selection;
    
    if (elementsSelected.length > 0) {
      var replacedCount = 0;
      
      for (var i = 0; i < elementsSelected.length; i++) {
        var targetItem = elementsSelected[i];
        
        // Check if the item is a text frame
        if (targetItem.typename == "TextFrame") {
          var currentText = targetItem.contents;
          var newText = currentText.replace(new RegExp(searchText, 'g'), replaceText);
          targetItem.contents = newText;
          replacedCount++;
        }
        
        // Deselect the item after processing
        targetItem.selected = false;
      }

      redraw();
      
      // Show a message with the number of replacements
      if (replacedCount > 0) {
        alert("已替换 " + replacedCount + " 个文本对象中的文本。");
      } else {
        alert("未找到匹配的文本进行替换。");
      }
    } else {
      alert("请先选中文本对象!");
    }
  } else {
    alert("请先打开一个文档!");
  }
} 